package org.dna.wickwtui.test;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.feedback.IFeedback;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.PropertyModel;
import org.dna.metronomo.persistence.User;
import org.dna.uimodel.SelectableUser;

/** input web page. */
public class FormInput extends WebPage {
	// holds NameWrapper elements
//	List<NameWrapper> data = Arrays.asList(new NameWrapper("one"),
//			new NameWrapper("two"), new NameWrapper("three"),
//			new NameWrapper("four"));
	
//	List<Boolean> data = Arrays.asList(new Boolean(false), new Boolean(false), new Boolean(false));
	
	List<SelectableUser> data = new ArrayList<SelectableUser>();
	
	public FormInput() {
		final FeedbackPanel feedback = new FeedbackPanel("feedback");
		add(feedback);
		add(new InputForm("inputForm", feedback));
	}

	/** form for processing the input. */
	private class InputForm extends Form {
		
		public InputForm(String name, IFeedback feedback) {
			super(name);
			data.add(new SelectableUser(new User("Mario 1", "1 pass", "role x")));
			data.add(new SelectableUser(new User("Mario 2", "2 pass", "role x")));
			data.add(new SelectableUser(new User("Mario 3", "2 pass", "role x")));
			
			// add a nested list view; as the list is nested in the form, the
			// form will
			// update all FormComponent childs automatically.
			ListView listView = new ListView("list", data) {
				protected void populateItem(ListItem item) {
					SelectableUser wrapper = (SelectableUser) item.getModelObject();
					item.add(new Label("name", wrapper.getName()));
					item.add(new CheckBox("check", new PropertyModel(wrapper,
							"selected")));
				}
			};
			listView.setReuseItems(true);
			add(listView);
		}

        @Override
		public void onSubmit() {
			info("data: " + data); // print current contents
		}
	}
}
